package com.jerry.huihome.module.find_near_mine.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.jerry.huihome.module.find.entity.DesignerDetail;

/**
 * Created by axiang on 2016/5/18.
 */
public class HuihomeDBHelper extends SQLiteOpenHelper{
    //数据库名称
    public static final String DB_NAME = "huihome.db";
    //数据库版本
    public static final  int DB_VERSION=3;

    public HuihomeDBHelper(Context context){
        super(context, DB_NAME, null, DB_VERSION);
    }


    //第一次创建数据库时调用
    @Override
    public void onCreate(SQLiteDatabase db){
        //创建表的脚本,版本为3,或3.x,都需要执行
        db.execSQL(HuihomeDB.HuihomeTable.SQL_CREATE_TABLE);
        //更新后的版本
        db.execSQL(HuihomeDB.HuihomeTable.SQL_ADD_TYPE_COLUMN);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        //改动DB_VERSION=3--->4;
        if(newVersion == 4){
            //添加判断是收藏还是浏览的字段
            db.execSQL(HuihomeDB.HuihomeTable.SQL_ADD_TYPE_COLUMN);

        }
    }
    /*
    * public static final String COLUMN_REGISTERNAME = "tvDesignersName";
    public static final String COLUMN_DESIGNERID = "designerid";
    public static final String COLUMN_RATINGBAR_COUNT = "ratingBar";
    public static final String COLUMN_SUCCESSNUM = "successNum";
    public static final String COLUMN_DESINMINFEE = "designMinFee";
    public static final String COLUMN_DESINMAXFEE = "designMaxFee";
    public static final String COLUMN_TOUXIANGURL = "touxiangurl";
    public static final String COLUMN_TYPE = "type";
    * */
    //收藏:
    public  boolean saveAttention(DesignerDetail designerDetail){
        ContentValues contentValues = new ContentValues();
        contentValues.put(HuihomeDB.HuihomeTable.COLUMN_DESIGNERID,designerDetail.getBody().getDesignerid());
        contentValues.put(HuihomeDB.HuihomeTable.COLUMN_REGISTERNAME,designerDetail.getBody().getDesignername());
        contentValues.put(HuihomeDB.HuihomeTable.COLUMN_RATINGBAR_COUNT,designerDetail.getBody().getTotalScore());
        contentValues.put(HuihomeDB.HuihomeTable.COLUMN_SUCCESSNUM,designerDetail.getBody().getCaseCount());
        contentValues.put(HuihomeDB.HuihomeTable.COLUMN_DESINMINFEE,designerDetail.getBody().getFeeLow());
        contentValues.put(HuihomeDB.HuihomeTable.COLUMN_DESINMAXFEE,designerDetail.getBody().getFeeHigh());
        contentValues.put(HuihomeDB.HuihomeTable.COLUMN_TOUXIANGURL,designerDetail.getBody().getAvatar());
        //自己自entity中添加type字段用来区别收藏or浏览
        contentValues.put(HuihomeDB.HuihomeTable.COLUMN_TYPE,designerDetail.getBody().getType());
        SQLiteDatabase db = getReadableDatabase();
        //作用:Convenience method for inserting a row into the database.
        //返回:return the row ID of the newly inserted row,
        // or -1 if an error occurred
        long rowId = db.insert(HuihomeDB.HuihomeTable.NAME, null, contentValues);
        if(rowId !=-1){
            return true;
        }
        return false;
    }
    //判断是否收藏or浏览
    //type 0>>收藏, 1>>浏览记录
    public boolean isCollected(int designerid,int type){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(HuihomeDB.HuihomeTable.NAME,
                new String[]{HuihomeDB.HuihomeTable.COLUMN_REGISTERNAME},
                HuihomeDB.HuihomeTable.COLUMN_DESIGNERID + "=? and " +
                HuihomeDB.HuihomeTable.COLUMN_TYPE + "=?",
                new String[]{designerid + "", type + ""},
                null, null, null);
        if(cursor.moveToNext()){
            return true;
        }
        return false;
    }
    private static HuihomeDBHelper huihomeDBHelper;
    public static HuihomeDBHelper getInstance(Context context){
        if(huihomeDBHelper ==null){
            synchronized(HuihomeDBHelper.class){
                huihomeDBHelper = new HuihomeDBHelper(context);
            }
        }
        return huihomeDBHelper;
    }
}
